package com.errol.batchprocessor.payroll.advisorimpl.other;

import com.renjia.enums.SwitchEnum;
import com.renjia.service.module.employeeaddition.advisorimpl.payroll.payload.PayrollEmployeeBatchPayload;
import com.renjia.service.module.payroll.advisor.upload.PayrollUploadCheckAdvisor;
import com.renjia.service.module.payroll.advisor.upload.PayrollUploadCreateAdvisor;
import com.renjia.service.module.payroll.advisor.verify.PayrollVerifyBatchStartAdvisor;
import com.renjia.service.module.payroll.advisorimpl.PayrollAdvisorTypeEnum;
import com.renjia.service.module.payroll.advisorimpl.payload.PayrollUploadBatchPayload;
import com.renjia.service.module.payroll.advisorimpl.payload.PayrollUploadSinglePayload;
import com.renjia.utils.DateUtil;
import org.springframework.lang.NonNull;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * 线下发放扩展实现
 *
 * @author zhang xiao bin
 * @version v1.0
 * @date 2021/03/06
 * @since v5.0.6
 **/
@Service
public class PayOfflineAdvisorImpl implements
        PayrollUploadCheckAdvisor, PayrollUploadCreateAdvisor,
        PayrollVerifyBatchStartAdvisor {

    @Override
    public boolean shallProcess(@NonNull PayrollEmployeeBatchPayload advisorPayload) {
        return PayrollAdvisorTypeEnum.OFFLINE == advisorPayload.getPayrollAdvisorTypeEnum();
    }

    @Override
    public int priority() {
        return 204;
    }

    @Override
    public boolean doCheckBeforePayrollSavedAtUploadStage(@NonNull PayrollUploadBatchPayload batchPayload) {
        // 检查线下发放检查是否提交了时间发放时间
        if (batchPayload.getPayrollRecordPo().getOfflinePayTime() == null) {
            batchPayload.getErrMsgList().add("线下发放请提交实际发放时间");
            return false;
        }
        return true;
    }

    @Override
    public boolean doAfterNewPayrollRecordAtUploadStage(@NonNull PayrollUploadBatchPayload batchPayload) {
        // v4.6.7.1 线下发放添加实际发放时间
        Date date = DateUtil.parseDate(batchPayload.getPayrollRecordPo().getOfflinePayTime(), "yyyy-MM-dd hh:mm:ss");
        batchPayload.getBatchEntity().setPayTime(date);
        batchPayload.getBatchEntity().setFinishTime(date);
        return true;
    }

    @Override
    public boolean doAfterNewPayrollDetailAtUploadStage(@NonNull PayrollUploadSinglePayload singlePayload, @NonNull PayrollUploadBatchPayload batchPayload) {
        singlePayload.getBusPayrollRecordDetail().setPayTime(batchPayload.getBatchEntity().getPayTime());
        singlePayload.getBusPayrollRecordDetail().setPayResultTime(batchPayload.getBatchEntity().getFinishTime());
        return true;
    }

    @Override
    public boolean doBeforeModifyPayrollDetailAtUploadStage(@NonNull PayrollUploadSinglePayload singlePayload, @NonNull PayrollUploadBatchPayload batchPayload) {
        return true;
    }

    @Override
    public boolean doPrepareOnBatchStartAtVerifyStage(@NonNull PayrollEmployeeBatchPayload batchPayload) {
        if (SwitchEnum.OFF.getCode2().equals(batchPayload.getCoreCustomerChannelMerchant().getCheckOn())) {
            batchPayload.setNeedCheckThreeElement(false);
        }
        return true;
    }
}
